package com.tianqu.tms.www.statistics.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TmsTableNames;
import com.tianqu.tms.www.statistics.dto.BusinessTransactionDto;
import com.tianqu.tms.www.statistics.entity.TmsSellerRegisterDayCountDto;
import com.tianqu.tms.www.statistics.entity.TmsSellerRegisterMonthCountDto;


public class TmsSellerRegisterMonthCountDao extends TmsDao{

	public TmsSellerRegisterMonthCountDao() throws TmsDaoException {
		super();
	}

	
	/**
	 * 当前月销售量/当前月收入量
	 * @return
	 * @throws TmsDaoException
	 */
	public BusinessTransactionDto getNowMonthSales() throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		BusinessTransactionDto businessTransactionDto = new BusinessTransactionDto();
		try {
			StringBuffer sql = new StringBuffer("  SELECT	");
			sql.append(" offtakeNum, ");
			sql.append(" buyNum ");
			sql.append(" FROM ");
			sql.append(TmsTableNames.TMS_SELLER_REGISTER_MONTH_COUNT);
			sql.append(" where countY = DATE_FORMAT(NOW(),'%Y') ");
			sql.append(" and countM = DATE_FORMAT(NOW(),'%c') ");
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			if (rs.next()) {
				// 当前月销售量
				businessTransactionDto.setOfftakeNumNow(rs.getBigDecimal("offtakeNum"));
				
				// 当前月收入量
				businessTransactionDto.setBuyNumNow(rs.getBigDecimal("buyNum"));
			}
			return businessTransactionDto;
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
	}
	
	/**
	 * 去年同期月销售量/去年同期月收车量
	 * @return
	 * @throws TmsDaoException
	 */
	public BusinessTransactionDto getLastYeasMonthSales() throws TmsDaoException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		BusinessTransactionDto businessTransactionDto = new BusinessTransactionDto();
		try {
			StringBuffer sql = new StringBuffer("  SELECT	");
			sql.append(" offtakeNum, ");
			sql.append(" buyNum ");
			sql.append(" FROM ");
			sql.append(TmsTableNames.TMS_SELLER_REGISTER_MONTH_COUNT);
			sql.append(" where countY = date_format(DATE_SUB(NOW(), INTERVAL 1 YEAR), '%Y') ");
			sql.append(" and countM = DATE_FORMAT(NOW(),'%c') ");
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			if (rs.next()) {
				
				// 去年同期月销售量
				businessTransactionDto.setOfftakeNumLastYeas(rs.getBigDecimal("offtakeNum"));
				// 去年同期月收车量
				businessTransactionDto.setBuyNumLastYeas(rs.getBigDecimal("buyNum"));
			}
			return businessTransactionDto;
			
		} catch (SQLException e) {
			throw new TmsDaoException(e.toString());

		} finally {
			super.close(rs, ps);
		}
	}
	
}
